/**
 * @USER: Administrator
 * @DATE: 2024/9/19 下午7:37
 * @VERSION: 1.0.0
 */
public class LC605 {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        int count = 0;
        int i=0;
        while (i < flowerbed.length - 1) {
            // 如果已种花，下一个位置必定不能种，直接跳过
            if (flowerbed[i] == 1) {
                i+=2;
            }else {
                flowerbed[i] = 1-flowerbed[i+1];
                if (flowerbed[i] == 1) {
                    count++;
                    i+=2;
                }else {
                    i++;
                }
            }
        }
        if (flowerbed.length>=2&&flowerbed[flowerbed.length-1] == 0 && flowerbed[flowerbed.length-2] == 0) {
            flowerbed[flowerbed.length-1] = 1;
            count++;
        }

        if (flowerbed.length==1 && flowerbed[0]==0){
            flowerbed[0] = 1;
            count++;
        }

        return count >= n;
    }
}
